package br.com.pd.messaging;

import java.util.ArrayList;
import java.util.List;

import br.com.pd.invocation.InvokerIF;
import br.com.pd.invocation.SupplyInvoker;
import br.com.util.PropertiesLoader;

public class ConsumerRequestHandle implements Listener, Subject, RequestHandleIF {

	private int port;
	private RequestReceiver requestReceiver;
	private RequestSender requestSender;
	private List<Listener> listeners;
	private InvokerIF invoker;
	
	
	public ConsumerRequestHandle(){
		this.port = Integer.parseInt(PropertiesLoader.getInstance().getPropertie(PropertiesLoader.PORT_SUPPLY_RECEIVER));
		this.invoker = new SupplyInvoker(this);
		this.requestSender = new RequestSender();
		this.requestReceiver = new RequestReceiver(this.port);
		this.requestReceiver.addObserver(this);
		this.listeners = new ArrayList<Listener>();
		this.requestSender.start();
		this.requestReceiver.start();
	}

	
	public void send(String b, String location, int port){
		this.requestSender.send(b, location, port);
	}
	
	
	@Override
	public String receive() {
		return this.requestReceiver.receive();
	}
	
	@Override
	public void update() {
		System.out.println("debug: chegou mensagem suply");
		this.invoker.execute(this.requestReceiver.receive());
	}


	@Override
	public void addObserver(Listener o) {
		this.listeners.add(o);
	}

	@Override
	public void removeObserver(Listener o) {
		this.listeners.remove(o);
	}
	


	

}
